//
//  NumberPickerCellData.m
//  18Birdies
//
//  Created by Lc on 15/5/14.
//  Copyright (c) 2015年 Lc. All rights reserved.
//

#import "LcFoundation.h"
#import "NumberPickerCell.h"
#import "NumberPickerCellData.h"

@implementation NumberPickerCellData

+ (NumberPickerCellData*)cellDataWithStartValue:(int)startValue
                                       endValue:(int)endValue
                                     unitSingle:(NSString*)unitSingle
                                     unitPlural:(NSString*)unitPlural
                                          value:(int)value {
  LC_CHECK_TRUE(endValue > startValue);
  NSMutableArray* values = [NSMutableArray array];
  for (int i = startValue; i <= endValue; ++i) {
    [values addObject:@(i)];
  }
  return [self cellDataWithValues:values
                       unitSingle:unitSingle
                       unitPlural:unitPlural
                            value:value];
}

+ (NumberPickerCellData*)cellDataWithValues:(NSArray*)values
                                 unitSingle:(NSString*)unitSingle
                                 unitPlural:(NSString*)unitPlural
                                      value:(int)value {
  LC_CHECK_NOT_EMPTY(values);
  int currentValue = INT32_MIN;
  for (NSNumber* number in values) {
    LC_CHECK_TRUE(number.intValue > currentValue);
    currentValue = number.intValue;
  }
  NumberPickerCellData* cellData = [[self alloc] init];
  cellData.values = values;
  cellData->_unitSingle = unitSingle.copy;
  cellData->_unitPlural = unitPlural.copy;
  cellData.value = value;
  cellData.clickable = NO;
  cellData.cellHeight = 162;
  return cellData;
}

- (Class)cellClazz {
  return NumberPickerCell.class;
}

- (NSString*)nibName {
  return @"NumberPickerCell";
}

- (void)refreshCell:(NumberPickerCell*)cell {
  [super refreshCell:cell];
  if (self.hidden) {
    return;
  }
  cell.pickerView.dataSource = self;
  cell.pickerView.delegate = self;
  [cell.pickerView selectRow:[self indexOfCurrentValue]
                 inComponent:0
                    animated:NO];
  [cell.pickerView reloadAllComponents];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView {
  return 1;
}

- (NSInteger)pickerView:(UIPickerView*)pickerView
    numberOfRowsInComponent:(NSInteger)component {
  return self.values.count;
}

- (NSString*)pickerView:(UIPickerView*)pickerView
            titleForRow:(NSInteger)row
           forComponent:(NSInteger)component {
  int value = [self.values[row] intValue];
  NSString* unit = nil;
  if (value == 1) {
    unit = self.unitSingle;
  } else {
    unit = self.unitPlural;
  }
  if (unit.length == 0) {
    if (self.transformZeroToOff && value == 0) {
      return @"关闭";
    } else {
      return [NSString stringWithFormat:@"%d", value];
    }
  } else {
    return [NSString stringWithFormat:@"%d %@", value, unit];
  }
}

- (void)pickerView:(UIPickerView*)pickerView
      didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component {
  self.value = [self.values[row] intValue];
    if ([self.delegate respondsToSelector:@selector(onNumberChanged:cellData:)]) {
      [self.delegate onNumberChanged:self.value cellData:self];
  }
}

- (int)clampValue:(int)value {
  LC_CHECK_NOT_EMPTY(self.values);
  NSNumber* lastValue = nil;
  for (NSNumber* number in self.values) {
    if (value == [number intValue]) {
      return value;
    } else if (value < [number intValue]) {
      if (lastValue == nil) {
        return [number intValue];
      } else {
        return [lastValue intValue];
      }
    }
    lastValue = number;
  }
  return [[self.values lastObject] intValue];
}

- (void)setValue:(int)value {
  if (self.values.count > 0) {
    value = [self clampValue:value];
  }
  _value = value;
}

- (int)indexOfCurrentValue {
  for (int i = 0; i < self.values.count; ++i) {
    NSNumber* number = self.values[i];
    if (self.value == [number intValue]) {
      return i;
    }
  }
  LC_FAIL();
  return 0;
}

@end
